package com.example.springmvc.exception;

import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;

import com.example.springmvc.common.ResultCode;
import org.springframework.lang.Nullable;
import org.springframework.util.*;

/**
 * 山寨自 {@link org.springframework.util.Assert}
 * <br>
 * 将自定义 message 改为可自定义的 {@link BusinessException}
 * <hr>
 * <p>
 * 充分利用【短路模式】进行参数验证, 配合全局异常处理, 统一返回错误结果
 * <br>
 * 默认为参数校验失败 {@link com.example.springmvc.common.ResultCode#PARAM_CHECK_FAIL} (code: 103)
 *
 * @author 凡行
 */
public abstract class Asserts {
    /**
     * 默认参数错误
     */
    private static final int DEFAULT_CODE = ResultCode.PARAM_CHECK_FAIL.getCode();

    /**
     * Assert a boolean expression, throwing an {@code BusinessException}
     * if the expression evaluates to {@code false}.
     * <p>Call {@link #isTrue} if you wish to throw an {@code BusinessException}
     * on an assertion failure.
     * <pre class="code">Asserts.state(id == null, "The id property must not already be initialized");</pre>
     *
     * @param expression a boolean expression
     * @param message    the exception message to use if the assertion fails
     * @throws BusinessException if {@code expression} is {@code false}
     */
    public static void state(boolean expression, String message) {
        state(expression, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert a boolean expression, throwing an {@code BusinessException}
     * if the expression evaluates to {@code false}.
     * <p>Call {@link #isTrue} if you wish to throw an {@code BusinessException}
     * on an assertion failure.
     * <pre class="code">
     * Asserts.state(id == null,
     *     () -&gt; "ID for " + entity.getName() + " must not already be initialized");
     * </pre>
     *
     * @param expression a boolean expression
     * @param ex         a supplier for the exception to use if the
     *                   assertion fails
     * @throws BusinessException if {@code expression} is {@code false}
     * @since 5.0
     */
    public static void state(boolean expression, Supplier<? extends BusinessException> ex) {
        if (!expression) {
            throw ex.get();
        }
    }

    /**
     * Assert a boolean expression, throwing an {@code BusinessException}
     * if the expression evaluates to {@code false}.
     * <pre class="code">Asserts.isTrue(i &gt; 0, "The value must be greater than zero");</pre>
     *
     * @param expression a boolean expression
     * @param message    the exception message to use if the assertion fails
     * @throws BusinessException if {@code expression} is {@code false}
     */
    public static void isTrue(boolean expression, String message) {
        isTrue(expression, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert a boolean expression, throwing an {@code BusinessException}
     * if the expression evaluates to {@code false}.
     * <pre class="code">
     * Asserts.isTrue(i &gt; 0, () -&gt; "The value '" + i + "' must be greater than zero");
     * </pre>
     *
     * @param expression a boolean expression
     * @param ex         a supplier for the exception to use if the
     *                   assertion fails
     * @throws BusinessException if {@code expression} is {@code false}
     * @since 5.0
     */
    public static void isTrue(boolean expression, Supplier<? extends BusinessException> ex) {
        if (!expression) {
            throw ex.get();
        }
    }

    /**
     * Assert that an object is {@code null}.
     * <pre class="code">Asserts.isNull(value, "The value must be null");</pre>
     *
     * @param object  the object to check
     * @param message the exception message to use if the assertion fails
     * @throws BusinessException if the object is not {@code null}
     */
    public static void isNull(@Nullable Object object, String message) {
        isNull(object, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert that an object is {@code null}.
     * <pre class="code">
     * Asserts.isNull(value, () -&gt; "The value '" + value + "' must be null");
     * </pre>
     *
     * @param object the object to check
     * @param ex     a supplier for the exception to use if the
     *               assertion fails
     * @throws BusinessException if the object is not {@code null}
     * @since 5.0
     */
    public static void isNull(@Nullable Object object, Supplier<? extends BusinessException> ex) {
        if (object != null) {
            throw ex.get();
        }
    }

    /**
     * Assert that an object is not {@code null}.
     * <pre class="code">Asserts.notNull(clazz, "The class must not be null");</pre>
     *
     * @param object  the object to check
     * @param message the exception message to use if the assertion fails
     * @throws BusinessException if the object is {@code null}
     */
    public static void notNull(@Nullable Object object, String message) {
        notNull(object, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert that an object is not {@code null}.
     * <pre class="code">
     * Asserts.notNull(clazz, () -&gt; "The class '" + clazz.getName() + "' must not be null");
     * </pre>
     *
     * @param object the object to check
     * @param ex     a supplier for the exception to use if the
     *               assertion fails
     * @throws BusinessException if the object is {@code null}
     * @since 5.0
     */
    public static void notNull(@Nullable Object object, Supplier<? extends BusinessException> ex) {
        if (object == null) {
            throw ex.get();
        }
    }

    /**
     * Assert that the given String is not empty; that is,
     * it must not be {@code null} and not the empty String.
     * <pre class="code">Asserts.hasLength(name, "Name must not be empty");</pre>
     *
     * @param text    the String to check
     * @param message the exception message to use if the assertion fails
     * @throws BusinessException if the text is empty
     * @see StringUtils#hasLength
     */
    public static void hasLength(@Nullable String text, String message) {
        hasLength(text, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert that the given String is not empty; that is,
     * it must not be {@code null} and not the empty String.
     * <pre class="code">
     * Asserts.hasLength(name, () -&gt; "Name for account '" + account.getId() + "' must not be empty");
     * </pre>
     *
     * @param text the String to check
     * @param ex   a supplier for the exception to use if the
     *             assertion fails
     * @throws BusinessException if the text is empty
     * @see StringUtils#hasLength
     * @since 5.0
     */
    public static void hasLength(@Nullable String text, Supplier<? extends BusinessException> ex) {
        if (!StringUtils.hasLength(text)) {
            throw ex.get();
        }
    }

    /**
     * Assert that the given String contains valid text content; that is, it must not
     * be {@code null} and must contain at least one non-whitespace character.
     * <pre class="code">Asserts.hasText(name, "'name' must not be empty");</pre>
     *
     * @param text    the String to check
     * @param message the exception message to use if the assertion fails
     * @throws BusinessException if the text does not contain valid text content
     * @see StringUtils#hasText
     */
    public static void hasText(@Nullable String text, String message) {
        hasText(text, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert that the given String contains valid text content; that is, it must not
     * be {@code null} and must contain at least one non-whitespace character.
     * <pre class="code">
     * Asserts.hasText(name, () -&gt; "Name for account '" + account.getId() + "' must not be empty");
     * </pre>
     *
     * @param text the String to check
     * @param ex   a supplier for the exception to use if the
     *             assertion fails
     * @throws BusinessException if the text does not contain valid text content
     * @see StringUtils#hasText
     * @since 5.0
     */
    public static void hasText(@Nullable String text, Supplier<? extends BusinessException> ex) {
        if (!StringUtils.hasText(text)) {
            throw ex.get();
        }
    }

    /**
     * Assert that the given text does not contain the given substring.
     * <pre class="code">Asserts.doesNotContain(name, "rod", "Name must not contain 'rod'");</pre>
     *
     * @param textToSearch the text to search
     * @param substring    the substring to find within the text
     * @param message      the exception message to use if the assertion fails
     * @throws BusinessException if the text contains the substring
     */
    public static void doesNotContain(@Nullable String textToSearch, String substring, String message) {
        doesNotContain(textToSearch, substring, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert that the given text does not contain the given substring.
     * <pre class="code">
     * Asserts.doesNotContain(name, forbidden, () -&gt; "Name must not contain '" + forbidden + "'");
     * </pre>
     *
     * @param textToSearch the text to search
     * @param substring    the substring to find within the text
     * @param ex           a supplier for the exception to use if the
     *                     assertion fails
     * @throws BusinessException if the text contains the substring
     * @since 5.0
     */
    public static void doesNotContain(@Nullable String textToSearch, String substring, Supplier<? extends BusinessException> ex) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
            textToSearch.contains(substring)) {
            throw ex.get();
        }
    }

    /**
     * Assert that an array contains elements; that is, it must not be
     * {@code null} and must contain at least one element.
     * <pre class="code">Asserts.notEmpty(array, "The array must contain elements");</pre>
     *
     * @param array   the array to check
     * @param message the exception message to use if the assertion fails
     * @throws BusinessException if the object array is {@code null} or contains no elements
     */
    public static void notEmpty(@Nullable Object[] array, String message) {
        notEmpty(array, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert that an array contains elements; that is, it must not be
     * {@code null} and must contain at least one element.
     * <pre class="code">
     * Asserts.notEmpty(array, () -&gt; "The " + arrayType + " array must contain elements");
     * </pre>
     *
     * @param array the array to check
     * @param ex    a supplier for the exception to use if the
     *              assertion fails
     * @throws BusinessException if the object array is {@code null} or contains no elements
     * @since 5.0
     */
    public static void notEmpty(@Nullable Object[] array, Supplier<? extends BusinessException> ex) {
        if (ObjectUtils.isEmpty(array)) {
            throw ex.get();
        }
    }

    /**
     * Assert that an array contains no {@code null} elements.
     * <p>Note: Does not complain if the array is empty!
     * <pre class="code">Asserts.noNullElements(array, "The array must contain non-null elements");</pre>
     *
     * @param array   the array to check
     * @param message the exception message to use if the assertion fails
     * @throws BusinessException if the object array contains a {@code null} element
     */
    public static void noNullElements(@Nullable Object[] array, String message) {
        notNull(array, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert that an array contains no {@code null} elements.
     * <p>Note: Does not complain if the array is empty!
     * <pre class="code">
     * Asserts.noNullElements(array, () -&gt; "The " + arrayType + " array must contain non-null elements");
     * </pre>
     *
     * @param array the array to check
     * @param ex    a supplier for the exception to use if the
     *              assertion fails
     * @throws BusinessException if the object array contains a {@code null} element
     * @since 5.0
     */
    public static void noNullElements(@Nullable Object[] array, Supplier<? extends BusinessException> ex) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw ex.get();
                }
            }
        }
    }

    /**
     * Assert that a collection contains elements; that is, it must not be
     * {@code null} and must contain at least one element.
     * <pre class="code">Asserts.notEmpty(collection, "Collection must contain elements");</pre>
     *
     * @param collection the collection to check
     * @param message    the exception message to use if the assertion fails
     * @throws BusinessException if the collection is {@code null} or
     *                           contains no elements
     */
    public static void notEmpty(@Nullable Collection<?> collection, String message) {
        notEmpty(collection, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert that a collection contains elements; that is, it must not be
     * {@code null} and must contain at least one element.
     * <pre class="code">
     * Asserts.notEmpty(collection, () -&gt; "The " + collectionType + " collection must contain elements");
     * </pre>
     *
     * @param collection the collection to check
     * @param ex         a supplier for the exception to use if the
     *                   assertion fails
     * @throws BusinessException if the collection is {@code null} or
     *                           contains no elements
     * @since 5.0
     */
    public static void notEmpty(@Nullable Collection<?> collection, Supplier<? extends BusinessException> ex) {
        if (CollectionUtils.isEmpty(collection)) {
            throw ex.get();
        }
    }

    /**
     * Assert that a Map contains entries; that is, it must not be {@code null}
     * and must contain at least one entry.
     * <pre class="code">Asserts.notEmpty(map, "Map must contain entries");</pre>
     *
     * @param map     the map to check
     * @param message the exception message to use if the assertion fails
     * @throws BusinessException if the map is {@code null} or contains no entries
     */
    public static void notEmpty(@Nullable Map<?, ?> map, String message) {
        notEmpty(map, () -> new BusinessException(DEFAULT_CODE, message));
    }

    /**
     * Assert that a Map contains entries; that is, it must not be {@code null}
     * and must contain at least one entry.
     * <pre class="code">
     * Asserts.notEmpty(map, () -&gt; "The " + mapType + " map must contain entries");
     * </pre>
     *
     * @param map the map to check
     * @param ex  a supplier for the exception to use if the
     *            assertion fails
     * @throws BusinessException if the map is {@code null} or contains no entries
     * @since 5.0
     */
    public static void notEmpty(@Nullable Map<?, ?> map, Supplier<? extends BusinessException> ex) {
        if (CollectionUtils.isEmpty(map)) {
            throw ex.get();
        }
    }
}
